// 导入ex模块
const express = require('express')
const { houselist,searchHouse,addhouse } = require('./house_data')
//2.创建路由对象
const router = express.Router()
// restfull的风格 获取的是所有的房屋的信息
router.get('/houselist', (request, response) => {
  // console.info('get方法获取到了', request.query)
  // console.info('get方法获取到了', request.params)
  let back = {
    "total": houselist.length,
    "rows": houselist,
    "code": 200,
    "msg": "查询成功"
  }
  response.send(back)
})
// 根据id 获取房屋数据
router.get('/gethouse/:id', (request, response) => {
  let house_id = request.params.id
  let house = houselist.find((item) => {
    return item.id ==house_id
  })
  let back = {
    "data": house,
    "code": 200,
    "msg": "查询成功"
  }
  response.send(back)
})
// 添加数据
router.get('/addhouse', (request, response) => {
  let model = JSON.parse(request.query.model) 
  model = addhouse(model)
  let back = {
    "data": model,
    "code": 200,
    "msg": "添加成功"
  }
  response.send(back)
})
// 分页获取数据 restfull 路径的参数  ?
router.get('/searchHouselist/:page/:size', (request, response) => {
  let page =  request.params.page
  let size =  request.params.size
  let searchlist = searchHouse(page,size,request.query)
  let back = {
    "total": searchlist.length,
    "rows": searchlist,
    "code": 200,
    "msg": "查询成功"
  }
  response.send(back)
})
//向外导出路由对象
module.exports = router